package com.huizluo.module4.copydir;

import java.io.*;

public class CopyFile implements Runnable{
    private File dst;
    private File src;

    public CopyFile(File dst, File src) {
        this.dst = dst;
        this.src = src;
    }


    @Override
    public void run() {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try{
            bis = new BufferedInputStream(new FileInputStream(this.src));
            bos = new BufferedOutputStream(new FileOutputStream(this.dst));
            System.out.printf("正在拷贝 [%s]\n",this.src.getName());
            byte[] buf = new byte[1024*8];
            int res = 0;
            while ((res = bis.read(buf))!=-1){
                bos.write(buf,0,res);
            }

            System.out.printf("[%s] 拷贝完成\n",this.src.getName());

        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if (null!=bos){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null!=bis){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
